package models

import (
	"log"
	"time"
	"xorm.io/builder"
)

type Task struct {
	Id                        int       `xorm:"not null pk autoincr INT(11)"`
	StudentId                 int       `xorm:"index INT(11)"`
	SchoolId                  int       `xorm:"index INT(11)"`
	Subject                   string    `xorm:"comment('科目') index ENUM('1','2','3','4')"`
	Validetime                int64     `xorm:"INT(11)"`
	Result                    string    `xorm:"comment('0不合格1合格2审核中') ENUM('0','1','2')"`
	Needtime                  int64     `xorm:"default 0 comment('需要补充的学时') INT(11)"`
	Needmileage               float64   `xorm:"default 0 comment('需要补学公里数') FLOAT"`
	Reason                    string    `xorm:"default '' comment('不合格的原因') VARCHAR(255)"`
	Checktime                 time.Time `xorm:"index DATETIME"`
	Validmileage              float32   `xorm:"default 0 comment('有效公里数') FLOAT"`
	Isreport                  string    `xorm:"comment('0未上报1已上报') ENUM('0','1')"`
	PdfId                     int64     `xorm:"comment('电子培训部分记录表PDF文件ID') index INT(11)"`
	CheckStatus               string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	RecheckStatus             string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	NeedNetworktime           int       `xorm:"default 0 comment('需要远程的学时') INT(11)"`
	NeedClasstime             int       `xorm:"default 0 comment('需要课堂的学时') INT(11)"`
	NeedVehicletime           int       `xorm:"default 0 comment('需要实车的学时') INT(11)"`
	NeedSimulatortime         int       `xorm:"default 0 comment('需要模拟器的学时') INT(11)"`
	UpdateTime                time.Time `xorm:"index DATETIME"`
	Duration                  int64     `xorm:"not null comment('审核合格学时') INT(11)"`
	TotalInvalidTime          int64     `xorm:"not null comment('本培训部分本次的审核不合格总学时，n分钟') INT(11)" json:"total_invalid_time"`
	TransNeedFulTime          int       `xorm:"not null comment('从业需补总学时') default(0)" json:"trans_need_ful_time"`
	TransNeedFulvehcleTime    int       `xorm:"not null comment('从业需补实车学时') default(0)" json:"trans_need_fulvehcle_time"`
	TransNeedFulNetWorkTime   int       `xorm:"not null default(0) comment('从业需补远程学时')" json:"trans_need_ful_net_work_time"`
	TransNeedFulSimulatorTime int       `xorm:"not null default(0) comment('从业需补模拟学时')" json:"trans_need_ful_simulator_time"`
	TransNeedFulClassTime     int       `xorm:"not null default(0) comment('从业需补课堂学时')" json:"trans_need_ful_class_time"`
}

type TaskList struct {
	Id              int    `json:"id"`
	Name            string `json:"name"`
	StudentId       int    `json:"student_id"`
	SchoolId        int    `json:"school_id"`
	SchoolName      string `json:"school_name"`
	Phone           string `json:"phone"`
	IdNumber        string `json:"id_number"`
	Subject         string `json:"subject"`
	TrainingCarType string `json:"training_car_type"`
	OperationStatus string `json:"operation_status"`
	CheckStatus     string `json:"check_status"`
	RecheckStatus   string `json:"recheck_status"`
	Province        string `json:"province"`
	City            string `json:"city"`
	Istransfer      string `json:"istransfer"`
	Isreport        string `json:"isreport"`
}

func (t *Task) GetTaskList(filter builder.Cond, pageSize, offset int) (list []TaskList, count int64, err error) {
	err = engine.Table("dts_task").Alias("t").
		Select("s.name_short as school_name,st.istransfer,t.isreport,s.id as school_id,st.id as student_id,t.id,t.recheck_status,t.check_status,u.name,u.phone,u.id_number,t.subject,st.operation_status,st.training_car_type,s.province,s.city").
		Join("LEFT", []string{"dts_student", "st"}, "st.id = t.student_id").
		Join("LEFT", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("LEFT", []string{"dts_school", "s"}, "s.id = t.school_id").
		And(filter).Limit(pageSize, offset).Desc("t.id").
		Find(&list)
	count, err = engine.Table("dts_task").Alias("t").
		Join("LEFT", []string{"dts_student", "st"}, "st.id = t.student_id").
		Join("LEFT", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("LEFT", []string{"dts_school", "s"}, "s.id = t.school_id").
		And(filter).Count()
	log.Println(err)
	return
}

type TaskDetail struct {
	StudentId         int     `json:"student_id"`
	Name              string  `json:"name"`
	SchoolName        string  `json:"school_name"`
	Needtime          string  `json:"needtime"`
	Needmileage       float64 `json:"needmileage"`
	Subject           string  `json:"subject"`
	Phone             string  `json:"phone"`
	IdNumber          string  `json:"id_number"`
	TrainingCarType   string  `json:"training_car_type"`
	OperationStatus   string  `json:"operation_status"`
	CheckStatus       string  `json:"check_status"`
	RecheckStatus     string  `json:"recheck_status"`
	Validetime        int     `json:"validetime"`
	Validmileage      float64 `json:"validmileage"`
	NeedReportTime    int     `json:"need_report_time"`
	NeedReportMileage float64 `json:"need_report_mileage"`
	Reason            string  `json:"reason"`
	Shicao            int     `json:"shicao"`
	Moni              int     `json:"moni"`
	Ketang            int     `json:"ketang"`
	Yuancheng         int     `json:"yuancheng"`
}

func (t *Task) GetById(id string) (has bool, list TaskDetail, err error) {
	has, err = engine.Table("dts_task").Alias("t").
		Select("s.name_short as school_name,st.id as student_id,t.reason,t.id,t.reason,t.needtime,t.needmileage,t.recheck_status,t.check_status,u.name,u.phone,u.id_number,t.subject,st.operation_status,st.training_car_type").
		Join("LEFT", []string{"dts_student", "st"}, "st.id = t.student_id").
		Join("LEFT", []string{"dts_user", "u"}, "u.id = st.user_id").
		Join("LEFT", []string{"dts_school", "s"}, "s.id = t.school_id").
		Where("t.id = ?", id).
		Get(&list)
	return
}

func (t *Task) Save() bool {
	if _, err := engine.Table("dts_task").Insert(t); err != nil {
		log.Println(err)
		return false
	}
	return true
}
